package com.cennavi.minenavidemo.activity.navi;

import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.cennavi.minenavidemo.R;
import com.cennavi.minenavidemo.activity.geocode.ChooseMapPointActivity;
import com.cennavi.minenavidemo.presenter.MapPresenter;
import com.cennavi.minenavidemo.widget.MyMapView;
import com.minedata.minenavi.addons.Fragment.RouteAndNavigationFragment;
import com.minedata.minenavi.map.CompassView;
import com.minedata.minenavi.map.MineMap;
import com.minedata.minenavi.map.MyLocationStyle;
import com.minedata.minenavi.map.ScaleView;
import com.minedata.minenavi.map.TrafficEventInfo;
import com.minedata.minenavi.mapdal.LatLng;
import com.minedata.minenavi.mapdal.PoiFavorite;
import com.minedata.minenavi.mapdal.Tools;
import com.minedata.minenavi.util.BackHandlerHelper;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 低成本接口
 */
public class NewInterfaceActivity extends AppCompatActivity {
    private final int QUERY_ROUTE_START = 1;
    private final int QUERY_ROUTE_END = 2;

    private MineMap mMineMap;
    private MyLocationStyle myLocationStyle;

    private String mStartName;
    private Point mStartPoint = new Point();

    // 终点
    private String mEndName;
    private Point mEndPoint = new Point();
    private MapPresenter mMapPresenter;

    private Unbinder mUnbinder;
    @BindView(R.id.mapView)
    MyMapView mMapView;
    @BindView(R.id.compassView)
    CompassView mCompassView;
    @BindView(R.id.scaleView)
    ScaleView mScaleView;
    @BindView(R.id.btn_entry_route)
    Button mEntryRouteBtn;
    @BindView(R.id.tv_start)
    TextView mStartTextView;
    @BindView(R.id.tv_end)
    TextView mEndTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_interface);
        mUnbinder = ButterKnife.bind(this);
        init();
    }

    private void addRouteFragment() {
        if (!Tools.isStringValid(mStartName)) {
            Toast.makeText(this, "请选择起起点", Toast.LENGTH_SHORT).show();
        } else if (!Tools.isStringValid(mEndName)) {
            Toast.makeText(this, "请选择起终点", Toast.LENGTH_SHORT).show();
        } else {
            mEntryRouteBtn.setVisibility(View.GONE);
            mStartTextView.setVisibility(View.GONE);
            mEndTextView.setVisibility(View.GONE);
            PoiFavorite startPoint = new PoiFavorite(mStartPoint, mStartName);
            PoiFavorite endPoint = new PoiFavorite(mEndPoint, mEndName);
            getSupportFragmentManager().beginTransaction().replace(R.id.layout_container, RouteAndNavigationFragment.newInstance(startPoint, endPoint, mMineMap)).commitAllowingStateLoss();
        }
    }

    public void init() {
        mMapPresenter = MapPresenter.getInstance();
        mMapPresenter.init(NewInterfaceActivity.this, mMapView);
        mMapPresenter.setOnMapViewListener(new MapPresenter.OnMapViewListener() {
            @Override
            public void onMapReady() {
                mMineMap = mMapPresenter.getMineMap();
                mMineMap.enableTrafficEventLayer(true);
                mMineMap.enableTmc(true);
                //mMineMap.setStyleClass("BLUE");
                //mMineMap.setStyleClass("BLUE,blue-ti");
                //mMineMap.setStyleClass("NAVIGATION-BLUE");
                // 设置地图中心点
//            Point point = new Point(12354878, 4183434);
//            mMineMap.setPointToCenter(point.x, point.y);

                mMapPresenter.addMyLocationStyle();

                mMapView.addListener(new MyMapView.Listener() {
                    @Override
                    public void onCameraChanged(int changeType) {
                        mCompassView.updateHeading(mMineMap.getHeading());
                        mScaleView.updateScale(mMineMap.getZoomLevel());
                    }

                    @Override
                    public void onTrafficEventIconClicked(int type, TrafficEventInfo trafficEventInfo) {
                        LatLng latLng = Tools.ndsPointToLatLng(trafficEventInfo.ndsPoint);
                        Toast.makeText(NewInterfaceActivity.this, latLng.latitude + " " + latLng.longitude, Toast.LENGTH_LONG).show();
                    }

                    @Override
                    public void onScrollStarted() {

                    }

                    @Override
                    public void onScrollFinished() {

                    }
                });
            }

            @Override
            public void onswitchLocation(int imageLevel) {

            }
        });

        mCompassView.setMapView(mMapView);
        mScaleView.setMapView(mMapView);
        mScaleView.setDrawPos(ScaleView.Pos.leftBottom);

    }

    @OnClick(R.id.btn_entry_route)
    public void route() {
        addRouteFragment();
    }

    @OnClick(R.id.tv_start)
    public void start() {
        startChooseMapPointActivity(QUERY_ROUTE_START);
    }

    @OnClick(R.id.tv_end)
    public void end() {
        startChooseMapPointActivity(QUERY_ROUTE_END);
    }

    @Override
    protected void onPause() {
        if (mMapView != null) {
            mMapView.onPause();
        }

        super.onPause();
    }

    @Override
    protected void onResume() {
        if (mMapView != null) {
            mMapView.onResume();
        }

        super.onResume();
    }

    @Override
    protected void onDestroy() {
        mMapPresenter.deleteMyLocationStyle();

        if (mMapView != null) {
            mMapView.onDestroy();
        }

        mUnbinder.unbind();
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_CANCELED) {
            return;
        }
        // 注意看这里用的是requestCode!!!!
        switch (requestCode) {
            case QUERY_ROUTE_START:
                mStartPoint.x = data.getIntExtra("poiX", 0);
                mStartPoint.y = data.getIntExtra("poiY", 0);
                mStartName = data.getStringExtra("name");
                mStartTextView.setText(mStartName);
                break;
            case QUERY_ROUTE_END:
                mEndPoint.x = data.getIntExtra("poiX", 0);
                mEndPoint.y = data.getIntExtra("poiY", 0);
                mEndName = data.getStringExtra("name");
                mEndTextView.setText(mEndName);
                break;
            default:
                break;
        }
    }

    private void startChooseMapPointActivity(int index) {
        Intent intent = new Intent(this, ChooseMapPointActivity.class);
        startActivityForResult(intent, index);
    }


    @Override
    public void onBackPressed() {
        if (!BackHandlerHelper.handleBackPress(this)) {
            super.onBackPressed();
        }
    }
}
